/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ConsultaExperta.java
 *
 * Created on 1/01/2005, 01:33:27 AM
 */

package zonasoft.Vistas.Negocio;

import com.mysql.jdbc.Connection;
import java.awt.Dimension;
import java.awt.Point;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import zonasoft.Conceptos.Lider;
import zonasoft.Conceptos.Zonificados;
import zonasoft.Manejadores.ManejadorBaseDatos;
import zonasoft.Manejadores.ManejadorLider;
import zonasoft.Manejadores.ManejadorQuerys;
import zonasoft.Manejadores.ManejadorZonificados;

/**
 *
 * @author Usuario
 */
public class Novedades extends javax.swing.JDialog {
    HashMap<String,Integer> departamentos;
    HashMap<String,Integer> municipios;
    /** Creates new form ConsultaExperta */
    public Novedades(java.awt.Frame parent) {
        super(parent, true);

        initComponents();
        pack();
        java.awt.Rectangle parentBounds = parent.getBounds();
        Dimension size = getSize();
        // Center in the parent
        int x = Math.max(0, parentBounds.x + (parentBounds.width - size.width) / 2);
        int y = Math.max(0, parentBounds.y + (parentBounds.height - size.height) / 2);
        setLocation(new Point(x, y));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jComboBox1 = new javax.swing.JComboBox();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jXTaskPaneContainer1 = new org.jdesktop.swingx.JXTaskPaneContainer();
        jLabel1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablaNovedad = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        totalSim = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
        aceptar = new javax.swing.JButton();
        limpiar = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        areaGeneral = new javax.swing.JTextArea();

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane2.setViewportView(jTextArea1);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jXTaskPaneContainer1.setBackground(new java.awt.Color(255, 51, 51));
        jXTaskPaneContainer1.setForeground(new java.awt.Color(255, 51, 51));

        jLabel1.setFont(new java.awt.Font("Vani", 1, 18));
        jLabel1.setText("Novedades ");
        jXTaskPaneContainer1.add(jLabel1);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED, java.awt.Color.darkGray, java.awt.Color.darkGray, java.awt.Color.lightGray, java.awt.Color.lightGray), "Intentos de Duplicacion de Datos ", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Lucida Bright", 1, 12))); // NOI18N

        tablaNovedad.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "C.C: Zonificado", "Lider Asociado","Lider Nuevo","Descripcion"
            }
        ));
        tablaNovedad.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tablaNovedadMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tablaNovedad);

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel2.setText("Total Novedades");

        totalSim.setEditable(false);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 700, Short.MAX_VALUE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(totalSim, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(totalSim, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(27, Short.MAX_VALUE))
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder(java.awt.Color.lightGray, java.awt.Color.lightGray));

        aceptar.setFont(new java.awt.Font("Tahoma", 1, 12));
        aceptar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/zonasoft/Vistas/iconos/ok 32.png"))); // NOI18N
        aceptar.setText("Aceptar");
        aceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                aceptarActionPerformed(evt);
            }
        });

        limpiar.setFont(new java.awt.Font("Tahoma", 1, 12));
        limpiar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/zonasoft/Vistas/iconos/limpiar.png"))); // NOI18N
        limpiar.setText("Limpiar");
        limpiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                limpiarActionPerformed(evt);
            }
        });

        jButton1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/zonasoft/Vistas/iconos/target-icon.png"))); // NOI18N
        jButton1.setText("Listar Novedades");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(aceptar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(limpiar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(limpiar, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(aceptar, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(50, 50, 50))
        );

        areaGeneral.setColumns(20);
        areaGeneral.setEditable(false);
        areaGeneral.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        areaGeneral.setRows(5);
        jScrollPane3.setViewportView(areaGeneral);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jXTaskPaneContainer1, javax.swing.GroupLayout.DEFAULT_SIZE, 1012, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane3, 0, 0, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(28, 28, 28))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jXTaskPaneContainer1, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(26, 26, 26)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(34, 34, 34))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
//        tablaNovedad.set(false);
        limpiarTabla();
        listaNovedad();
       
    }//GEN-LAST:event_jButton1ActionPerformed

    private void limpiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_limpiarActionPerformed
        // TODO add your handling code here:
        limpiarTabla();
//        limpiarTablaZ();
//        textAreaDatosVotacion.setText("");
}//GEN-LAST:event_limpiarActionPerformed

    private void aceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aceptarActionPerformed
        // TODO add your handling code here:
        this.dispose();
}//GEN-LAST:event_aceptarActionPerformed

    private void tablaNovedadMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tablaNovedadMouseClicked
        // TODO add your handling code here:
        ManejadorBaseDatos mb = ManejadorBaseDatos.getInstancia();
//List<String> n = new ArrayList<Strign>();
        int indice = 1;
        try {
            mb.conectar();
        } catch (Exception ex) {
            Logger.getLogger(Novedades.class.getName()).log(Level.SEVERE, null, ex);
        }
        ManejadorLider.conm = mb.getConexion();
        ManejadorZonificados.con = mb.getConexion();
        if (evt.getClickCount() == 1) {
            int fila = tablaNovedad.rowAtPoint(evt.getPoint());
            int columna = tablaNovedad.columnAtPoint(evt.getPoint());
            if ((fila > -1) && (columna > -1)) {
//                if (columna == 0) {
                columna = 1;
                int columna1 = columna - 1;
                int columna2 = columna + 1;
                Lider l = null;
                Lider ln = null;
                Zonificados z = null;
//                System.out.println("columna1 = " + columna1);
//                System.out.println("sl = " + l);
//                 id = Integer.parseInt(tablaNovedad.getValueAt(fila, columna).toString());

                String id = (tablaNovedad.getValueAt(fila, columna).toString()).trim();
                String idZ = (tablaNovedad.getValueAt(fila, columna1).toString()).trim();
//                String idN = (tablaNovedad.getValueAt(fila, columna).toString()).trim();
//                System.out.println("id = " + idN);
                try {
                    if (id == "") { //Validacion que posea un lider asociado
                        areaGeneral.setText("");
                    } else {
                        l = ManejadorLider.getLider(String.valueOf(id));
                        System.out.println("id = " + id);
                        z = ManejadorZonificados.getZonificadosPropio(String.valueOf(idZ));
                        System.out.println("idZ = " + idZ);
//                        ln = ManejadorLider.getLider(String.valueOf(idN));
//                        System.out.println("idN = " + idN);
//                        System.out.println("idZ = " + idZ);
//                        System.out.println("id = " + id );
                        if (l != null && z != null) {
                            System.out.println("z = " + z);
                            String nombre = l.getNombre().toUpperCase();
                            String apellido = l.getApellido().toUpperCase();
//                    System.out.println("nombre = " + z.getNombres());    System.out.println("apellido = " + z.getApellidos());
                            String nombreZ = z.getNombres().toUpperCase();
                            String apellidoZ = z.getApellidos().toUpperCase();
                            if( ln != null){
                                String nombreL= ln.getNombre().toUpperCase();
                            String apellidoL = ln.getApellido().toUpperCase();
                            areaGeneral.setText("->Zonificado: " + "\n" + nombreZ + " " + apellidoZ + "\n" + "->Lider Asociado: " + "\n" + nombre + " " + apellido +"\n"+ "->Lider Nuevo: "+"\n"+nombreL + " "+ apellidoL);
                            }else{
                            areaGeneral.setText("->Zonificado: " + "\n" + nombreZ + " " + apellidoZ + "\n" + "->Lider: " + "\n" + nombre + " " + apellido);
                        }
                         } else {
                            // fin de la comparacion, del objeto si es vacio
//                     areaGeneral.setText(" '<html> <h2 Style='color: red; font-style: italic;'> Ningun lider asociado a esta identificacion </h2>'"  );
                            areaGeneral.setText("->Zonificado: " + "\n" + z.getNombres().toUpperCase() + " " +z.getApellidos().toUpperCase() + "\n" + "Ningun dato asociado " + "\n" + " a esta identificacion");
                        }
                    }//valido el que venga vacio
                } catch (Exception ex) {
                    Logger.getLogger(Novedades.class.getName()).log(Level.SEVERE, null, ex);
                    ex.printStackTrace();
                }
            } else {
                JOptionPane.showMessageDialog(null, "Por favor de click en el id del Lider");
            }
//            }
        }

    }//GEN-LAST:event_tablaNovedadMouseClicked

    /**
    * @param args the command line arguments
   
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                ConsultaExperta dialog = new ConsultaExperta(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    } */


        public void limpiarTabla() {
        DefaultTableModel dtm = (DefaultTableModel) tablaNovedad.getModel();
        dtm.setRowCount(0);
        totalSim.setText("");
        areaGeneral.setText("");
    }

        
    
 public void listaNovedad(){
     
     ManejadorBaseDatos mbd = ManejadorBaseDatos.getInstancia();
     ManejadorQuerys.con = (Connection) mbd.getConexion();
     List<String> novedades = null;
	 DefaultTableModel dtm = (DefaultTableModel)tablaNovedad.getModel();
	 try{
	   novedades =   ManejadorQuerys.novedades();
           tablaNovedad.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
           tablaNovedad.setFont(new java.awt.Font("Tahoma",0,13));
             TableColumn tc = tablaNovedad.getColumn("C.C: Zonificado");
             tc.setPreferredWidth(106);
             TableColumn tc1 = tablaNovedad.getColumn("Lider Asociado");
             tc1.setPreferredWidth(106);
              TableColumn tc2 = tablaNovedad.getColumn("Lider Nuevo");
             tc2.setPreferredWidth(106);
              TableColumn tc3 = tablaNovedad.getColumn("Descripcion");
             tc3.setPreferredWidth(506);
             for (String dato : novedades) {
                 String[]nov=dato.split(",");
                 Object[]fila={nov[1],nov[2],nov[3],nov[6]};
                 dtm.addRow(fila); 
             }//,nov[4],nov[5],nov[6]
             int r = dtm.getRowCount();
                totalSim.setText(String.valueOf(r));
                
//            }
	 }catch(Exception ex){
	  Logger.getLogger(Novedades.class.getName()).log(Level.SEVERE, null, ex);
	 }
 
     }
      
      
      
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton aceptar;
    private javax.swing.JTextArea areaGeneral;
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTextArea jTextArea1;
    private org.jdesktop.swingx.JXTaskPaneContainer jXTaskPaneContainer1;
    private javax.swing.JButton limpiar;
    private javax.swing.JTable tablaNovedad;
    private javax.swing.JTextField totalSim;
    // End of variables declaration//GEN-END:variables

}
